<?php
  include("include/config.inc.php");
  if(!isset($_SESSION['s_activId']))
  {
    header("location:checklogin.php");
    exit();
  }
  if(!(isset($_SESSION['ses_branchId']) && $_SESSION['ses_branchId'] > 0))
  {
  	header("Location: index.php");
  }
  else
  {
    $branchIdSelected = "";
    $fromIdSession    = $_SESSION['ses_branchName'];
    $toIdSession      = $_SESSION['ses_branchId'];
    $loadinginvoiceId = $_REQUEST['loadingInvoiceId'];
    
    //Display For Master Record
    $selectLoding = "SELECT invoiceNo, invoiceDate, truckNo, branchName
                       FROM loadinginvoice 
                       JOIN truckmaster ON truckmaster.truckId = loadinginvoice.truckId
                       JOIN  branch  ON branch.branchId = loadinginvoice.toId
                      WHERE loadingInvoiceId = ".$loadinginvoiceId;
    $selectLodingRes = mysql_query($selectLoding);
    $lodingRow = mysql_fetch_array($selectLodingRes);
    
    //Insert Loding Invoice 
    if(isset($_POST['submit']))
    {      
    	$loadinginvoiceId = $_REQUEST['loaddingId'];  
      $loopCntDetail = 0;
      while($loopCntDetail < count($_POST['lrNo']))
      {
        $lrNo                     = isset($_POST['lrNo'][$loopCntDetail]) ? $_POST['lrNo'][$loopCntDetail] : "";
        $qty                      = isset($_POST['qty'][$loopCntDetail]) ? $_POST['qty'][$loopCntDetail] : 0;
        $discription              = isset($_POST['discription'][$loopCntDetail]) ? $_POST['discription'][$loopCntDetail] : "";
        $discriptionPartyDetailId = isset($_POST['discriptionPartyDetailId'][$loopCntDetail]) ? $_POST['discriptionPartyDetailId'][$loopCntDetail] : 0;
        $consignorPartyId         = isset($_POST['consignorPartyId'][$loopCntDetail]) ? $_POST['consignorPartyId'][$loopCntDetail] : 0;
        $consignorName            = isset($_POST['consignor'][$loopCntDetail]) ? $_POST['consignor'][$loopCntDetail] : "";
        $consignee                = isset($_POST['consignee'][$loopCntDetail]) ? $_POST['consignee'][$loopCntDetail] : "";
        $lrDate                   = isset($_POST['lrDate'][$loopCntDetail*3+2]) ? 
                                      $_POST['lrDate'][$loopCntDetail*3+2]['Year']."-".$_POST['lrDate'][$loopCntDetail*3+1]['Month']."-".$_POST['lrDate'][$loopCntDetail*3]['Day']
                                      : '0000-00-00';
        $weight                   = strlen($_POST['weight'][$loopCntDetail]) > 0 ? $_POST['weight'][$loopCntDetail] : 0;
        $toPay                    = isset($_POST['toPay'][$loopCntDetail]) ? $_POST['toPay'][$loopCntDetail] : "";
        $remarks                  = isset($_POST['remarks'][$loopCntDetail]) ? $_POST['remarks'][$loopCntDetail] : 0;
        $consignor                = isset($_POST['consignor'][$loopCntDetail]) ? $_POST['consignor'][$loopCntDetail] : ""; 
        $billType                 = isset($_POST['billType'][$loopCntDetail]) ? $_POST['billType'][$loopCntDetail] : ""; 
        
       
        if($qty > 0)
        {
        	if($discription != '')
          {
          	$insertItem = "INSERT INTO partydetail (item)
                                   VALUES ('".$discription."')";
            $insertItemRes = mysql_query($insertItem);
          }
          if($discriptionPartyDetailId > 0)
          {
        	  $selectItemName = "SELECT partyDetailId,item
        	                       FROM partydetail
        	                      WHERE partyDetailId = ".$discriptionPartyDetailId;
        	  $selectItemNameRes = mysql_query($selectItemName);
        	  if($itemCRow = mysql_fetch_array($selectItemNameRes))
        	  {
        	  	$discription = $itemCRow['item'];
        	  }
        	}
          if($consignorName != '')
          {
          	$insertParty = "INSERT INTO party (name)
                                   VALUES ('".$consignorName."')";
            $insertPartyRes = mysql_query($insertParty);
          }
          if($consignorPartyId > 0)
          {
        	  $selectPartyName = "SELECT partyId,name
        	                        FROM party
        	                       WHERE partyId = ".$consignorPartyId;
        	  $selectPartyNameRes = mysql_query($selectPartyName);
        	  if($partyCRow = mysql_fetch_array($selectPartyNameRes))
        	  {
        	  	$consignorPartyId = $partyCRow['partyId'];
        	  	$consignorName    = $partyCRow['name'];
        	  }
        	}
        	  
          $insertregisterbill = "INSERT INTO loadinginvoicedetail (loadingInvoiceId,billType,lrNo,qty,discriptionPartyDetailId,discription,consignorPartyId,consignor,consignee,lrDate,weight,toPay,remarks)
                                        VALUES(".$loadinginvoiceId.",'".$billType."',".$lrNo.",".$qty.",'".$discriptionPartyDetailId."','".$discription."',
                                               '".$consignorPartyId."','".$consignorName."','".$consignee."','".$lrDate."',".$weight.",'".$toPay."',
                                               '".$remarks."')";
          $insertregisterbillRes = mysql_query($insertregisterbill);
          if(!$insertregisterbillRes)
          {
          	echo "data not Inserted ".$insertregisterbill.mysql_error();
          }
          else
          {
          	//header("Location:loadinginvoice.php");
          }
        }
        $loopCntDetail ++; 
      }
      header('location:loadinginvoice.php');
    }
    $truckDataArray = array();
    $i = 0;
    $selecttruck = "SELECT truckNo,truckId
                    FROM truckmaster
                ORDER BY truckNo";
    $selecttruckResult = mysql_query($selecttruck);
    while($truckdRow = mysql_fetch_array($selecttruckResult))
    {
    	$truckDataArray['truckNo'][$i]     = $truckdRow['truckNo'];
    	$truckDataArray['truckId'][$i]     = $truckdRow['truckId'];
    	$i++;
    }
     $k=0;
    $branchArray = array();
    $selectBranch = "SELECT branchId,branchName
                       FROM branch
                       WHERE branchId != ".$_SESSION['ses_branchId'];
    $selectBranchRes = mysql_query($selectBranch);
    while($branchRow = mysql_fetch_array($selectBranchRes))
    {
    	$branchArray['branchId'][$k]   = $branchRow['branchId'];
    	$branchArray['branchName'][$k] = $branchRow['branchName'];
    	$k++;
    }
    
    $j=0;
    $loadingArray = array();
    $selectLoadingInvoice = "SELECT loadingInvoiceId,invoiceNo,DATE_FORMAT(invoiceDate,'%d-%m-%Y') AS invoiceDate,totalWeight,
                                    fromTable.branchName AS fromName,truckNo,toTable.branchName AS toName,loadinginvoice.fromId,toId
                               FROM loadinginvoice
                          LEFT JOIN branch AS fromTable ON fromTable.branchId = loadinginvoice.fromId
                          LEFT JOIN branch AS toTable ON toTable.branchId = loadinginvoice.toId
                          LEFT JOIN truckmaster ON truckmaster.truckId = loadinginvoice.truckId
                              WHERE fromId = ".$toIdSession."";
    $selectLoadingInvoiceRes = mysql_query($selectLoadingInvoice);
    while($loadingRow = mysql_fetch_array($selectLoadingInvoiceRes))
    {
      $loadingArray[$j]['loadingInvoiceId']  = $loadingRow['loadingInvoiceId'];
      $loadingArray[$j]['fromName']          = $loadingRow['fromName'];
      $loadingArray[$j]['toName']            = $loadingRow['toName'];
      $loadingArray[$j]['truckNo']           = $loadingRow['truckNo'];
      $loadingArray[$j]['invoiceNo']         = $loadingRow['invoiceNo'];
      $loadingArray[$j]['totalWeight']       = $loadingRow['totalWeight'];
      $loadingArray[$j]['invoiceDate']       = $loadingRow['invoiceDate'];
      $j++;
    }
    
    $topayValueArray[0]  = "To Pay";
    $topayOutputArray[0] = "To Pay";
    $topayValueArray[1]  = "Paid";
    $topayOutputArray[1] = "Paid";
    
    $l=0;
    $itemArray = array();
    $selectItem = "SELECT partyDetailId,item
                          FROM partydetail";
    $selectItemRes = mysql_query($selectItem);
    while($itemRow = mysql_fetch_array($selectItemRes))
    {
    	$itemArray['partyDetailId'][$l]   = $itemRow['partyDetailId'];
    	$itemArray['item'][$l]            = $itemRow['item'];
    	$l++;
    }  
    
    $m=0;
    $PartyArray = array();
    $selectParty = "SELECT partyId,name
                      FROM party";
    $selectPartyRes = mysql_query($selectParty);
    while($partyRow = mysql_fetch_array($selectPartyRes))
    {
    	$PartyArray['partyId'][$m]   = $partyRow['partyId'];
    	$PartyArray['name'][$m]      = $partyRow['name'];
    	$m++;
    }   
    $printValueArray[1] = "wob";
    $printOutputArray[1] = "Without Bill";
    $printValueArray[0] = "wb";
    $printOutputArray[0] = "With Bill";
    
    include("./bottom.php");
    $smarty->assign('branchIdSelected',$_SESSION['ses_branchId']);
    $smarty->assign('branchNameSelected',$_SESSION['ses_branchName']);
    $smarty->assign('truckDataArray',$truckDataArray);
    $smarty->assign('loadingArray',$loadingArray);
    $smarty->assign('lodingRow',$lodingRow);
    $smarty->assign('loadinginvoiceId',$loadinginvoiceId);
    $smarty->assign('branchArray',$branchArray);
    $smarty->assign('topayOutputArray',$topayOutputArray);
    $smarty->assign('topayValueArray',$topayValueArray);
    $smarty->assign("itemArray",$itemArray);  
    $smarty->assign("PartyArray",$PartyArray);  
    $smarty->assign("printValueArray",$printValueArray);  
    $smarty->assign("printOutputArray",$printOutputArray);  
    $smarty ->display("loadingInvoiceAdd.tpl");
}
?>
